package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.exception.BadException;
import com.lwc.cfdns.pojo.bo.system.DeptBO;
import com.lwc.cfdns.pojo.dto.system.DeptDTO;
import com.lwc.cfdns.pojo.entity.SystemAdmin;
import com.lwc.cfdns.pojo.entity.SystemDept;
import com.lwc.cfdns.pojo.vo.system.DeptTreeVO;
import com.lwc.cfdns.pojo.vo.system.DeptVO;
import com.lwc.cfdns.service.SystemDeptService;
import com.lwc.cfdns.mapper.SystemDeptMapper;
import com.lwc.cfdns.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_dept(部门表)】的数据库操作Service实现
 * @createDate 2023-05-24 17:09:18
 */
@Service
public class SystemDeptServiceImpl extends ServiceImpl<SystemDeptMapper, SystemDept>
    implements SystemDeptService{

    /**
     * 创建
     *
     * @param deptDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:16:28
     */
    @Override
    public Map<String, Object> create(DeptDTO deptDTO) {
        Long count = this.baseMapper.selectCount(new LambdaQueryWrapper<SystemDept>().eq(SystemDept::getName, deptDTO.getName()));
        if (count > 0) {
            throw new BadException("部门已存在!");
        }
        SystemDept systemDept = new SystemDept();
        BeanUtils.copyProperties(deptDTO, systemDept);
        this.baseMapper.insert(systemDept);
        Map<String, Object> map = new HashMap<>();
        map.put("id", systemDept.getId());
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return DeptVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:19:38
     */
    @Override
    public DeptVO view(Long id) {
        SystemDept systemDept = this.baseMapper.selectById(id);
        Assert.notNull(systemDept, ResultCode.NOT_DATA_FOUNT);
        DeptVO deptVO = new DeptVO();
        BeanUtils.copyProperties(systemDept, deptVO);
        return deptVO;
    }

    /**
     * 修改
     *
     * @param id
     * @param deptDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:22:58
     */
    @Override
    public Map<String, Object> update(Long id, DeptDTO deptDTO) {
        SystemDept systemDept = this.baseMapper.selectById(id);
        // 不存在
        Assert.notNull(systemDept, ResultCode.NOT_DATA_FOUNT);
        HashMap<String, Object> map = new HashMap<>();
        BeanUtils.copyProperties(deptDTO, systemDept, "created_at", "creator_id");
        systemDept.setId(id);
        int i = this.baseMapper.updateById(systemDept);
        map.put("result", i);
        return map;
    }

    /**
     * 删除
     *
     * @param id
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:25:34
     */
    @Override
    public Map<String, Object> delete(Long id) {
        SystemDept systemDept = this.baseMapper.selectById(id);
        // 不存在
        Assert.notNull(systemDept, ResultCode.NOT_DATA_FOUNT);
        int i = this.baseMapper.deleteById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param deptBo
     * @return List<DeptVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/24 17:45:17
     */
    @Override
    public List<DeptVO> lists(DeptBO deptBo) {
        ArrayList<DeptVO> lists = new ArrayList<>();
        List<SystemDept> systemDepts = this.baseMapper.selectList(deptBo.toQueryWrapper());
        for (SystemDept systemDept : systemDepts) {
            DeptVO deptVO = new DeptVO();
            BeanUtils.copyProperties(systemDept, deptVO);
            lists.add(deptVO);
        }
        return lists;
    }

    /**
     * 树
     *
     * @param deptBO
     * @return List<DeptTreeVO>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 15:00:03
     */
    @Override
    public List<DeptTreeVO> tree(DeptBO deptBO) {
        List<DeptTreeVO> tree = new ArrayList<>();
        List<SystemDept> systemDepts = this.baseMapper.selectList(deptBO.toQueryWrapper());
        for (SystemDept systemDept : systemDepts) {
            if (systemDept.getPId().equals(0L)) {
                DeptTreeVO deptTreeVO = new DeptTreeVO();
                BeanUtils.copyProperties(systemDept, deptTreeVO);
                deptTreeVO.setLabel(systemDept.getName());

                tree.add(findChildren(systemDepts, deptTreeVO));
            }
        }
        return tree;
    }

    /**
     * 递规查找子
     *
     * @param systemDepts
     * @param deptTreeVO
     * @return DeptTreeVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/26 15:12:06
     */
    private <T extends DeptTreeVO> T findChildren(List<SystemDept> systemDepts, T deptTreeVO) {
        for (SystemDept systemDept : systemDepts) {
            if (deptTreeVO.getId().equals(systemDept.getPId())) {
                DeptTreeVO deptTreeVO2 = new DeptTreeVO();
                BeanUtils.copyProperties(systemDept, deptTreeVO2);
                deptTreeVO2.setLabel(systemDept.getName());

                deptTreeVO.getChildren().add(findChildren(systemDepts, deptTreeVO2));
            }
        }
        return deptTreeVO;
    }
}




